import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class OptimizedLineSimilarity {

    public static void main(String[] args) {
        String file1 = "单词本摘录文本历史记录.txt"; // 替换为您的文本1文件路径
        String file2 = "in.txt"; // 替换为您的文本2文件路径

        try {
            double duplicateRate = calculateDuplicateRate(file1, file2);
            System.out.println("文本2在文本1中的查重率: " + duplicateRate * 100 + "%");
        } catch (IOException e) {
            System.err.println("读取文件时发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }

    // 计算查重率
    private static double calculateDuplicateRate(String file1Path, String file2Path) throws IOException {
        // List<String> lines1 = new ArrayList<>(List.of(readFile(file1Path).toLowerCase().split("\n")));
    List<String> lines1 = new ArrayList<>(Arrays.asList(readFile(file1Path).toLowerCase().split("\n"))); // 修改此行
        String[] lines2 = readFile(file2Path).toLowerCase().split("\n");

        double totalSimilarity = 0.0;
        for (String line2 : lines2) {
            double maxSimilarity = 0.0;
            int maxIndex = -1;
            for (int i = 0; i < lines1.size(); i++) {
                if (line2.equals(lines1.get(i))) {
                    maxSimilarity = 1.0; // 直接比较相同
                    maxIndex = i;
                    break;
                }
                double similarity = calculateWordSimilarity(lines1.get(i), line2);
                if (similarity == 1.0) { // 单词相似度为100%
                    maxSimilarity = 1.0;
                    maxIndex = i;
                    break;
                }
                if (similarity > maxSimilarity) {
                    maxSimilarity = similarity;
                    maxIndex = i;
                }
            }
            totalSimilarity += maxSimilarity;
            if (maxSimilarity >= 0.95 && maxIndex != -1) {
                lines1.remove(maxIndex);
            }
        }

        if (lines2.length == 0) {
            return 0.0;
        }

        return totalSimilarity / lines2.length;
    }

    // 读取文件内容
    private static String readFile(String filePath) throws IOException {
        StringBuilder sb = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
        }
        return sb.toString();
    }

    // 计算两个句子之间的单词相似度
    private static double calculateWordSimilarity(String line1, String line2) {
        String[] words1 = line1.split("[\\s\\p{Punct}]+");
        String[] words2 = line2.split("[\\s\\p{Punct}]+");

        int commonWords = 0;
        for (String word2 : words2) {
            for (String word1 : words1) {
                if (word1.equals(word2)) {
                    commonWords++;
                    break;
                }
            }
        }

        return (double) commonWords / Math.max(words1.length, words2.length);
    }
}
